# -*-coding:utf-8 -*-
from django.shortcuts import render
from core.api.serializers import *
from core.models import *
from asset.models import BoardBox
from asset.api.serializers import BoardBoxSerializer
from rest_framework import generics, viewsets, renderers
from rest_framework.decorators import api_view
from rest_framework.views import APIView
from rest_framework import filters, permissions
from org.models import *
from org.api.serializers import *
from rest_framework.response import Response
from rest_framework.exceptions import NotFound
from core.api.views import DynamicFieldsViewMixin, HtmlApiMixin
from django.conf import settings


@api_view(['GET', ])
def t_portal_view(request, pk, format=None):
    """
    盒子/应用常用信息
    """
    try:
        obj = BoardBox.objects.get(pk=pk)
    except BoardBox.DoesNotExist:
        raise NotFound()

    serializer = BoardBoxSerializer(obj, many=False)
    department = DepartmentSerializer(obj.department).data
    corp = CorporationSerializer(obj.corp).data
    termianl = serializer.data
    data = {
        "termianl": termianl,
        "department": department,
        "welcome": termianl['welcome'] or corp['welcome'],
        "viewmode": termianl['viewmode'] or corp['viewmode'],
        "shutdown_time": termianl['shutdown_time'] or corp['shutdown_time'],
        "bg_image": termianl['bg_image'] or corp['bg_image']
    }
    return Response(data)


@api_view(['GET', ])
def resource_stat(request, format=None):
    """
    资源统计
    """
    box_boardbox = BoardBox.objects.stat_status()
    data = {
        "box_boardbox": box_boardbox
    }
    return Response(data)


class MapView(APIView, HtmlApiMixin):
    """
    沙盘模式地图
    """
    permission_classes = (permissions.IsAuthenticated, )
    title = u'沙盘模式地图'
    template_name = 'api/default_map.html'
    renderer_classes = (renderers.TemplateHTMLRenderer, renderers.BrowsableAPIRenderer, renderers.JSONRenderer)

    def get_html_context(self):
        context = super(MapView, self).get_html_context()
        new_context = {
            'gis': getattr(settings, "GIS", {}) or {}
        }
        return new_context

    def get(self, request, format=None):
        content = self.get_html_context()
        return Response(content)
